<template>
	<view class="content">
		<view class="logistics-box flexWrapNo"  @tap="goPage('/pages/shop/logistics?order_id='+order_id)" v-if="orderInfo.orderstate==0&&orderInfo.paystate==1&&orderInfo.shippingstate==1">
			<image src="../../static/shop/logistics-icon.png" class="logistics-icon"></image>
			<view class="logistics-info">
				<view class="info-top flexWrap">
					<view>{{expressname||'--'}}（{{expressno||'--'}}）</view>
					<view class="status">配送中</view>
				</view>
				<!-- <view class="info-cont">
					<view class="info-item">
						卖家已发货 2023-11-21 11:31:11
					</view>
					<view class="info-item">
						到达成都中转站  2023-11-22 11:31:11
					</view>
					<view class="info-item">
						到达点点花园中驿站  2023-11-22 11:31:11
					</view>
				</view> -->
			</view>
		</view>
		<view class="address-box flexWrap">
			<view class="address-left flexWrapNo">
				<image src="../../static/shop/address-icon.png" class="address-icon"></image>
				<view class="address-info">
					<view class="address-name">{{orderInfo.address||'--'}}</view>
					<view class="address-details">{{orderInfo.receiver||'--'}} {{orderInfo.mobile||'--'}}</view>
				</view>
			</view>
		</view>
		<view class="shop-box">
			<view class="top-box flexWrap">
				<view class="date">{{createtime}}</view>
				<block  v-if="orderInfo.paystate==0">
					<text class="status" v-if="orderInfo.orderstate==0">待支付</text>
					<text class="status" v-if="orderInfo.orderstate==1">已取消</text>
					<text class="status" v-if="orderInfo.orderstate==2">已失效</text>
				</block>
				<block v-else>
					<block v-if="orderInfo.orderstate==0">
						<text class="status" v-if="orderInfo.shippingstate==0">待发货</text>
						<text class="status" v-if="orderInfo.shippingstate==1">待收货</text>
						<text class="status" v-if="orderInfo.shippingstate==2">已收货</text>
					</block>
					<text class="status" v-if="orderInfo.orderstate==3">已完成</text>
					<text class="status" v-if="orderInfo.shippingstate==4">退货退款中</text>
				</block>	
			</view>
			<view class="shop-cont">
				<view class="shop-item flexWrapNo" v-for="(item,index) in goodInfo" :key="index">
					<image :src="item.image" class="shop-img"></image>
					<view class="shop-info-box flexWarpColumn">
						<view class="info-box">
							<view>{{item.title||'--'}}</view>
							<view class="specs-box">{{item.attrdata||'默认'}} , x{{item.nums||'--'}}</view>
						</view>
						<view class="flexWrap">
							<view class="price">¥{{item.price||'--'}}</view>
							<view class="opreate-box flexWrapNo" v-if="orderInfo.paystate==1">
								<view class="sale-btn" v-if="item.is_sales==1&&!item.after_sale_id&&!(orderInfo.orderstate==0&&orderInfo.paystate==1&&orderInfo.shippingstate==2)" @tap="goPage('/shop-mall/applyAftesales?orderId='+orderInfo.id+'&orderGoodsId='+item.id)">申请售后</view>
								<view class="sale-btn" v-if="item.after_sale_id" @tap="goPage('/shop-mall/aftesalesDetails?afterSalesId='+item.after_sale_id)">售后详情</view>
								<view class="sale-btn" v-if="orderInfo.shippingstate==2||orderInfo.orderstate==1||orderInfo.orderstate==2" 
									@tap="goPage('/pages/shop/shopDetails?goodsId='+item.goods_id)">再次购买</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="total-box">
				共{{nums}}件 总额 <text class="price">¥{{orderInfo.amount||'--'}}</text>
			</view>
		</view>
		<view class="order-box">
			<view class="order-info">
				订单编号:{{orderInfo.order_sn}}
			</view>
			<view class="order-info">
				支付方式:微信支付
			</view>
			<view class="order-info">
				下单时间:{{createtime}}
			</view>
			<block v-if="(orderInfo.orderstate==0&&orderInfo.paystate==1&&orderInfo.shippingstate==1)||(orderInfo.orderstate==3&&orderInfo.paystate==1)">
				<view class="order-info">
					物流公司:{{orderInfo.expressname}}
				</view>
				<view class="order-info">
					快递单号: {{orderInfo.expressno}}
				</view>
				<view class="order-info">
					发货时间:{{expiretime}}
				</view>
			</block>
			<view class="order-info" v-if="orderInfo.orderstate==0&&orderInfo.paystate==1&&orderInfo.shippingstate==0">
				成交时间:{{paytime}}
			</view>
		</view>
		<view class="bottom-box flexWrap">
			<block v-if="orderInfo.orderstate==0&&orderInfo.paystate==0">
				<view class="btn" @tap="shopCancelOrder()">取消订单</view>
				<view class="define-btn" @tap="getWechatPayInfo()">立即支付</view>
			</block>
			<!-- <block v-if="orderInfo.orderstate==0&&orderInfo.paystate==1&&orderInfo.shippingstate==0">
				<view class="define-btn" @tap="shopCancelOrder()">取消订单</view>
			</block> -->
			<block v-if="orderInfo.orderstate==0&&orderInfo.paystate==1&&orderInfo.shippingstate==1">
				<view class="btn" @tap="takedelivery()">确认收货</view>
				<view class="define-btn" @tap="goPage('/pages/shop/logistics?order_id='+order_id)">查看物流</view>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderId:"",
				orderInfo:{},
				goodInfo:{},
				createtime:"",
				paytime:"",
				nums:"",
				order_id:"",
				expressno:"",
				expressname:"",
				expiretime:""
			};
		},
		onLoad(ops) {
			this.orderId=ops.orderId
			this.getOrderInfo()
		},
		methods:{
			getOrderInfo(){
				this.$request.post(this.$api.shopOrderDetails, {
					ordersn:this.orderId
				}, {
					token:true
				}).then(res => {
					if (res.code == 1) {
						let {
							data = {}
						} = res;
						this.orderInfo=data
						this.goodInfo=data.order_goods
						this.createtime=this.$tools.formatDate(data.createtime)
						this.paytime=this.$tools.formatDate(data.paytime)
						this.expiretime=this.$tools.formatDate(data.expiretime)
						this.nums=this.goodInfo[0].nums
						this.order_id=data.id
						this.expressno=data.expressno
						this.expressname=data.expressname
					}
				})
			},
			// 取消订单
			shopCancelOrder(){
				this.$request.post(this.$api.shopCancelOrder, {
					ordersn:this.orderId
				}, {
					token:true
				}).then(res => {
					if (res.code == 1) {
						this.$tools.showToast("取消成功");
						setTimeout(() => {
							let pages = getCurrentPages(); //当前页
							let beforePage = pages[pages.length - 2]; //上个页面
							beforePage.$vm.freshList();
							uni.navigateBack({
								delta:1
							})
						},500)
					}
				})
			},
			// 确认收货
			takedelivery(){
				this.$request.post(this.$api.takedelivery, {
					ordersn:this.orderId
				}, {
					token:true
				}).then(res => {
					if (res.code == 1) {
						let {
							data = {}
						} = res;
						this.$tools.showToast("确认成功");
						this.getOrderInfo();
					}
				})
			},
			// 获取支付信息
			getWechatPayInfo(){
				this.$request.post(this.$api.wechatPay, {
					ordersn:this.orderId
				}, {
					token:true
				}).then(res => {
					if (res.code==1) {
						let {data}=res
						this.appletPay(data)
					}
				})
			},
			// 小程序支付
			appletPay(data) {
				let that = this
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: data.timeStamp,
					nonceStr: data.nonceStr,
					package: data.package,
					signType: 'MD5',
					paySign: data.sign,
					success(res) {
						that.$tools.showToast("支付成功");
						setTimeout(() => {
							this.getOrderInfo();
						},500)
					},
					fail(err) {
						console.log(err);
						that.$tools.showToast('支付取消')
					},
					complete(result) {
						
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.content{
		padding: 44upx 36upx;
		padding-bottom: 200upx;
	}
	.logistics-box{
		background: #1D1C5C;
		border-radius: 16rpx;
		padding: 32upx 28upx;
		align-items: flex-start;
		.logistics-icon{
			width: 36upx;
			height: 32upx;
			margin-right: 24upx;
			flex: none;
		}
		.logistics-info{
			.info-top{
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				.status{
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #FF4646;
				}
			}
			.info-cont{
				margin-top: 12upx;
				.info-item{
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color:#8E8EAE;
					line-height: 40upx;
				}
			}
		}
	}
	.address-box{
		background: #1D1C5C;
		border-radius: 16rpx;
		padding: 32upx 28upx;
		margin-top: 20upx;
		.address-left{
			align-items: start;
			.address-icon{
				width: 34upx;
				height: 40upx;
				flex: none;
			}
			.address-info{
				margin-left: 26upx;
				.address-name{
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
				.address-details{
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color:#8584A0;
					margin: 12upx 0;
				}
				.user-info{
					font-size: 24rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color:#8584A0;
				}
			}
		}
		.icon-more{
			border-color: #FFFFFF;
		}
	}
	.shop-box{
		background: #1D1C5C;
		border-radius: 16rpx;
		margin-top: 36upx;
		.top-box{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			padding: 32upx 28upx;
		}
		.shop-cont{
			padding: 32upx 28upx;
			border-top: 1rpx solid #36355E;
			border-bottom: 1rpx solid #36355E;
			.shop-item{
				margin-bottom: 20upx;
				&:nth-last-of-type(1){
					margin-bottom: 0;
				}
			}
			.shop-img{
				width: 164rpx;
				height: 164rpx;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				flex: none;
			}
			.shop-info-box{
				margin-left: 28upx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color:#FFFFFF;
				height: 164rpx;
				justify-content: space-between;
				flex: 1;
				.info-box{
					.specs-box{
						color: #8786A8;
						margin-top: 12upx;
					}
				}
				.opreate-box{
					justify-content: flex-end;
					gap: 10rpx;
					.sale-btn{
						width: 120rpx;
						height: 48rpx;
						background: #26BCFD;
						border-radius: 6rpx;
						font-size: 24rpx;
						color: #FFFFFF;
						text-align: center;
						line-height: 48rpx;	
					}
				}
			}
		}
		.total-box{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color:#8786A8;
			line-height: 100upx;
			text-align: right;
			padding: 0 28upx;
			.price{
				color: #FFFFFF;
				margin-left: 24upx;
			}
		}
	}
	.order-box{
		margin-top: 20upx;
		padding: 32upx 0;
		.order-info{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color:#82819B;
			margin-bottom: 20upx;
		}
	}
	.bottom-box{
		height: 120rpx;
		background: #1D1C5C;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		padding: 0 66upx;
		justify-content: center;
		.btn{
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			width: 282rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 16rpx;
			border: 2rpx solid #26BCFD;
			text-align: center;
			margin-right: 30upx;
		}
		.define-btn{
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 80rpx;
			width: 282rpx;
			height: 80rpx;
			background: #26BCFD;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			text-align: center;
		}
	}
</style>
